﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class FrontEnd_Control_Contents_Content
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'xu ly jquery khi post back
            If Page.RouteData.Values("MenuId") <> "" Then
                hidContent.Value = Page.RouteData.Values("MenuId")
            Else
                If hidContent.Value = "" Then
                    hidContent.Value = ConfigurationManager.AppSettings("MenuIdIndex") 'Id Menu hien thi cho trang index
                End If
            End If
            BindMenu()
        End If
    End Sub
    Protected Sub BindMenu(Optional ByVal iPage As Integer = 1)
        Using data As New VSEntities
            'So ban ghi muon the hien tren trang
            Dim intPag_Size As Int32 = 10
            Dim _content = (From p In data.Contents Where p.StatusId = 3 Order By p.PublishedDate Descending Select p).Skip((iPage - 1) * intPag_Size).Take(intPag_Size).ToList

            hidCount.Value = (From p In data.Contents Where p.StatusId = 3 Order By p.Created Descending Select p).Count
            With rptContent
                .DataSource = _content
                .DataBind()
            End With
          

            Create_Pager(hidCount.Value, iPage, intPag_Size, 5)

        End Using
    End Sub

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptContent_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptContent.ItemDataBound
        Using data As New VSEntities
            Dim row = CType(e.Item.DataItem, Content)
            Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = MainDomain & "m/" & row.QueryString & "-" & row.Id & ".html"
            hplTitle.Text = row.Title
            hplTitle.CssClass = "badge-evt badge-track"

            Dim hplImage As HyperLink = CType(e.Item.FindControl("hplImage"), HyperLink)
            hplImage.NavigateUrl = MainDomain & "m/" & row.QueryString & "-" & row.Id & ".html"

            'neu la video TypeId = 2 se hien thi video
            Dim _video = (From p In data.Contents Where p.Id = row.Id And p.TypeId = 2 Select p).FirstOrDefault

            Dim imgBig As Image = CType(e.Item.FindControl("imgBig"), Image)
            Dim imgIconPlay As Image = CType(e.Item.FindControl("imgIconPlay"), Image)
            If Not _video Is Nothing Then
                'Dim _UrlVideo = row.URL
                Dim _SubUrlVideo = row.URL.Trim().Substring(62, 11)
                imgBig.ImageUrl = "http://i2.ytimg.com/vi/" & _SubUrlVideo & "/0.jpg"
                imgBig.CssClass = "badge-item-img"
                imgBig.AlternateText = row.Title

                imgIconPlay.ImageUrl = MainDomain & "images/icon_play.png"
                imgIconPlay.CssClass = "videoIndicator"
            Else
                imgBig.ImageUrl = CookedImageURL("500-375", row.Image)
                imgBig.CssClass = "badge-item-img"
                imgBig.AlternateText = row.Title
            End If


            '<iframe width="560" height="315" src="//www.youtube.com/embed/h5Um8m7I2iY?rel=0" frameborder="0" allowfullscreen></iframe>
            '<img border="0" src="http://i2.ytimg.com/vi/h5Um8m7I2iY/0.jpg" height="0" width="0" /> code lay hinh youtube
            'share facebook
            Dim ltrShareFacebook As Literal = CType(e.Item.FindControl("ltrShareFacebook"), Literal)
            ltrShareFacebook.Text = "<a href='javascript:void(0);' class='badge-facebook-share btn-share facebook' data-evt='Facebook-Share,PostClicked," & hplTitle.NavigateUrl & "' data-share='" & hplTitle.NavigateUrl & "'>Facebook</a>"
            'share twitter
            Dim ltrShareTwitter As Literal = CType(e.Item.FindControl("ltrShareTwitter"), Literal)
            ltrShareTwitter.Text = "<a href='javascript:void(0);' class='badge-twitter-share btn-share twitter' data-evt='Twitter-Share,ListClicked," & hplTitle.NavigateUrl & "' data-title = '" & row.Title & "' data-share='" & hplTitle.NavigateUrl & "'>Twitter</a>"


            '<a class="comment badge-evt" href="gag/aNorA8G.html#comment" target="_blank" data-evt="EntryAction,CommentLinkUnderTitle,ListPage" >143 comments</a>
            Dim hplComment As HyperLink = CType(e.Item.FindControl("hplComment"), HyperLink)
            hplComment.NavigateUrl = MainDomain & "m/" & row.QueryString & "-" & row.Id & ".html#comment"
            hplComment.CssClass = "comment badge-evt"
            hplComment.Target = "_blank"
            hplComment.Attributes.Add("data-evt", "EntryAction,CommentLinkUnderTitle,ListPage")

            Dim hplCommentCount As HyperLink = CType(e.Item.FindControl("hplCommentCount"), HyperLink)
            hplCommentCount.NavigateUrl = MainDomain & "m/" & row.QueryString & "-" & row.Id & ".html#comment"
            hplCommentCount.CssClass = "comment badge-evt"
            hplCommentCount.Target = "_blank"
            hplCommentCount.Attributes.Add("data-evt", "EntryAction,CommentLinkUnderTitle,ListPage")
        End Using
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.CssClass = "active"
            lnkTitle.Text = e.Item.DataItem
        Else
            lnkTitle.Text = e.Item.DataItem
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        hidCur_Page.Value = lnkTile.ToolTip
        BindMenu(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkPrevStep_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPrevStep.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindMenu(hidCur_Page.Value)
    End Sub
    Protected Sub lnkNextStep_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkNextStep.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        If hidCur_Page.Value >= hidIndex_page.Value Then
            hidCur_Page.Value = hidIndex_page.Value
        End If
        BindMenu(hidCur_Page.Value)
    End Sub

    Protected Sub lnkFirst_Click(sender As Object, e As System.EventArgs) Handles lnkFirst.Click
        hidCur_Page.Value = 1
        BindMenu(hidCur_Page.Value)
    End Sub

    Protected Sub lnkLast_Click(sender As Object, e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidIndex_page.Value
        BindMenu(hidCur_Page.Value)
    End Sub
End Class
